package com.dlxj.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.dlxj.entity.Pole;
import com.dlxj.service.PoleService;
import com.dlxj.util.MyJSON;
import com.dlxj.util.PageBean;
import com.opensymphony.xwork2.ActionSupport;

public class PoleAction extends ActionSupport {

	PoleService poleService;
	Pole pole;
	String json;
	String id;
	String spid;
	String epid;
	String cid;
	int code;
	int pageIndex;
	int pageSize;
	
	
	public String findAll(){
		List<Pole> poles = poleService.findPoleList();
		json = MyJSON.toJson(poles ,"circuitriesForStartpoleNo","circuitriesForEndpoleNo").toString();

		return "success";
		
	}
	
	
	public String showPole(){
		Pole polez=null;
		if(!id.equals("0")){
		    polez=new Pole();
			polez.setId(id);
		}
		PageBean bean =  poleService.showPole(polez,pageIndex,pageSize);
		json = MyJSON.toJson(bean, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";
	}
	
	
	public String findPoleSort(int pageIndex,
			int pageSize){
		PageBean bean = poleService.findPoleSort(pageIndex, pageSize);
		json = MyJSON.toJson(bean, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";
	}
	
	
	public String add() {
		int code = poleService.addPole(pole);
		json = "{code:" + code + "}";
		return "success";

	}

	public String findList() {
		
		List<Pole> polies = poleService.findPoleList();
		json = MyJSON.toJson(polies, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();
		return "success";

	}

	public String delPole() {
		int code = poleService.delPoleById(id);
		json = "{code:" + code + "}";
		return "success";

	}

	public String findPoleById() {

		Pole pole = poleService.findPoleById(id);

		json = MyJSON.toJson(pole, "circuitriesForStartpoleNo",
				"circuitriesForEndpoleNo").toString();

		return "success";

	}

	public String findPoliesByCidAndSPidAndEPid() {

		json = MyJSON
				.toJson(poleService.findPoliesByStartPoleIdAndEndStartPoleIdAndCircuitryId(
						spid, epid, cid), "circuitriesForStartpoleNo",
						"circuitriesForEndpoleNo").toString();

		return "success";
	}

	public String findHasBug() {
		json = MyJSON.toJson(poleService.findHasBug(),
				"circuitriesForStartpoleNo", "circuitriesForEndpoleNo")
				.toString();
		return "success";

	}

	public String updatePersonInfo() {
		try {
			pole.setId(getPole().getId());
			 code = poleService.update(pole);
			json = "{code:" + code + "}";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";

	}

	public int getPageIndex() {
		return pageIndex;
	}




	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}




	public int getPageSize() {
		return pageSize;
	}




	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}




	public Pole getPole() {
		return pole;
	}

	public void setPole(Pole pole) {
		this.pole = pole;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public PoleService getPoleService() {
		return poleService;
	}

	public void setPoleService(PoleService poleService) {
		this.poleService = poleService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSpid() {
		return spid;
	}

	public void setSpid(String spid) {
		this.spid = spid;
	}

	public String getEpid() {
		return epid;
	}

	public void setEpid(String epid) {
		this.epid = epid;
	}

	public String getCid() {
		return cid;
	}

	public void setCid(String cid) {
		this.cid = cid;
	}

}
